/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Collection;
import jeeobserver.server.settings.Settings;

/**
 * The Class JeeObserverClient.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public class JeeObserverClient {

	 private final String serverHost;

	 private final int serverPort;

	 /**
	  * Instantiates a new jee observer client.
	  *
	  * @param serverHost the server host
	  * @param serverPort the server port
	  */
	 public JeeObserverClient(String serverHost, int serverPort) {

			this.serverHost = serverHost;
			this.serverPort = serverPort;
	 }

	 /**
	  * Load settings.
	  *
	  * @return the settings
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 public Settings loadSettings() throws JeeObserverServerException {

			final Object result = this.doRequest(new SettingsParameters.LoadParameter());

			if (result instanceof Settings) {
				 return (Settings) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Update settings.
	  *
	  * @param settings the settings
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 public void updateSettings(Settings settings) throws JeeObserverServerException {
			this.doRequest(new SettingsParameters.UpdateParameter(settings));
	 }

	 /**
	  * Searchaction statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<ActionStatistics.TotalStatistics> searchActionTotalStatistics(ActionParameters.SearchStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<ActionStatistics.TotalStatistics>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Searchaction statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<ActionStatistics.TimeStatistics> searchActionTimeStatistics(ActionParameters.SearchStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<ActionStatistics.TimeStatistics>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Searchaction elements.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<ActionStatistics.Element> searchActionElements(ActionParameters.SearchElementsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<ActionStatistics.Element>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Search jvm elements.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<JvmStatistics.Element> searchJvmElements(JvmParameters.SearchElementsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<JvmStatistics.Element>) result;
			}
			if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			}

			throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
	 }

	 /**
	  * Search jvm time statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<JvmStatistics.TimeStatistics> searchJvmTimeStatistics(JvmParameters.SearchStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<JvmStatistics.TimeStatistics>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Search jvm time statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<JvmStatistics.TotalStatistics> searchJvmTotalStatistics(JvmParameters.SearchStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<JvmStatistics.TotalStatistics>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Search http session total statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<HttpSessionStatistics.TotalStatistics> searchHttpSessionTotalStatistics(HttpSessionParameters.SearchStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<HttpSessionStatistics.TotalStatistics>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Search http session time statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<HttpSessionStatistics.TimeStatistics> searchHttpSessionTimeStatistics(HttpSessionParameters.SearchStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<HttpSessionStatistics.TimeStatistics>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Search http session elements.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<HttpSessionStatistics.Element> searchHttpSessionElements(HttpSessionParameters.SearchElementsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<HttpSessionStatistics.Element>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Search hard disk total statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<HardDiskStatistics.TotalStatistics> searchHardDiskTotalStatistics(HardDiskParameters.SearchStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<HardDiskStatistics.TotalStatistics>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Search hard disk time statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<HardDiskStatistics.TimeStatistics> searchHardDiskTimeStatistics(HardDiskParameters.SearchStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<HardDiskStatistics.TimeStatistics>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Search hard disk elements.
	  *
	  * @param parameter the parameter
	  *
	  * @return the list
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 @SuppressWarnings("unchecked")
	 public Collection<HardDiskStatistics.Element> searchHardDiskElements(HardDiskParameters.SearchElementsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Collection<?>) {
				 return (Collection<HardDiskStatistics.Element>) result;
			} else if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			} else {
				 throw new JeeObserverServerException("JeeObserver client invocation resulting object mismatch.");
			}
	 }

	 /**
	  * Deleteaction statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 public void deleteActionStatistics(ActionParameters.DeleteStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			}
	 }

	 /**
	  * Delete jvm statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 public void deleteJvmStatistics(JvmParameters.DeleteStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			}
	 }

	 /**
	  * Delete http session statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 public void deleteHttpSessionStatistics(HttpSessionParameters.DeleteStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			}
	 }

	 /**
	  * Delete hard disk statistics.
	  *
	  * @param parameter the parameter
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 public void deleteHardDiskStatistics(HardDiskParameters.DeleteStatisticsParameter parameter) throws JeeObserverServerException {

			final Object result = this.doRequest(parameter);

			if (result instanceof Exception) {
				 throw new JeeObserverServerException((Exception) result);
			}
	 }

	 /**
	  * Do request.
	  *
	  * @param object the object
	  *
	  * @return the object
	  *
	  * @throws JeeObserverServerException the server exception
	  */
	 public Object doRequest(Object object) throws JeeObserverServerException {

			try {
				 final Socket socket = new Socket(this.serverHost, this.serverPort);
				 final ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
				 final ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());

				 objectOutputStream.writeObject(object);
				 objectOutputStream.flush();

				 final Object result = objectInputStream.readObject();

				 objectOutputStream.close();
				 objectInputStream.close();

				 socket.close();

				 return result;

			} catch (final IOException e) {
				 throw new JeeObserverServerException("Error connecting to server on host: " + this.serverHost + " and port: " + this.serverPort, e);
			} catch (final ClassNotFoundException e) {
				 throw new JeeObserverServerException(e);
			}
	 }
}
